//package com.ta.gulimall.order.config;
//
////定义一个类，发送消息，接收消息，并进行消息确认回调
//@Service
//public class OrderProduceService implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {
//
//
//    @Autowired
//    private RabbitTemplate rabbitTemplate;
//
//    @PostConstruct
//    public void setCallback() {
//        rabbitTemplate.setReturnCallback(this);
//        rabbitTemplate.setConfirmCallback(this);
//        Runnable runnable=()->send("这是我发送的测试消息，测试id="+UUID.randomUUID().toString());
//        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
//        scheduledExecutorService.scheduleAtFixedRate(runnable,20,5, TimeUnit.SECONDS);
//    }
//
//
//    public void send(Object message) {
//        CorrelationData correlationData = new CorrelationData(UUID.randomUUID().toString());
//        rabbitTemplate.convertAndSend(RabbitConstants.ORDER_EXCHANGE, RabbitConstants.ORDER_ROUTE_KEY, message, correlationData);
//    }
//
//
//    // confirm确认消息投递成功了
//    //如果没有 Exchange ack=false,
//    //     * 如果有 Exchange ack=tru
//    @Override
//    public void confirm(CorrelationData correlationData, boolean ack, String s) {
//        System.out.println("消息发送成功,发送ack确认,id="+correlationData.getId());
//        if (ack){
//            System.out.println("发送成功");
//        }else {
//            System.out.println("发送失败");
//        }
//    }
//
//    //Exchange 到Queue投递成功，不回调ReturnCallback
//    @Override
//    public void returnedMessage(Message message, int i, String s, String s1, String s2) {
//        System.out.println("消息丢失, 没有投递成功");
//    }
//
//
//    // 监听订单队列
//    @RabbitListener(queues = RabbitConstants.ORDER_QUEUE)
//    public void orderQueueListener(Message message,Channel channel) throws IOException {
//        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
//        String msg = new String(message.getBody());
//        System.out.println("路由key= [ "+receivedRoutingKey+" ]接收到的消息= [ "+msg +" ]");
//        //Thread.sleep(5000);
//        //发送ack给消息队列,收到消息了
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
//    }
//
//    // 监听死信队列
//    @RabbitListener(queues = RabbitConstants.DEAD_QUEUE)
//    public void deadQueueListener(Message message, Channel channel) throws InterruptedException, IOException {
//        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
//        String msg = new String(message.getBody());
//        System.out.println("路由key= [ "+receivedRoutingKey+" ]接收到的消息= [ "+msg +" ]");
//        //Thread.sleep(5000);
//        // 发送ack给消息队列，收到消息了
//        channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
//
//    }
//}
//
